import javax.swing.*;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Scanner;

/**
 * 登录功能
 * 请输入用户名:花千骨
 * 请输入密码:123456
 * SQL语句: SELECT nickname FROM userinfo WHERE username=? AND password=?
 */
public class JDBCLogin {
    public static void main(String[] args) {
        //1.接收用户输入
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入用户名:");
        String username = scanner.nextLine();
        System.out.println("请输入密码:");
        String password = scanner.nextLine();

        //2.数据库操作
        try (Connection connection = DBUtil.getConnection();){
            Statement s1 = connection.createStatement();
            String sql = "SELECT nickname FROM userinfo WHERE username='"+username+"' AND password='"+password+"'";
            ResultSet resultSet = s1.executeQuery(sql);
            if (resultSet.next()){
                //登录成功
                String nickname = resultSet.getString("nickname");
                System.out.println("欢迎你:" + nickname);
            }else{
                System.out.println("用户名或密码错误");
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }

    }
}
